package com.we823.cms.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.FrontUtils;
import com.we823.cms.model.extension.ProductCategory;
import com.we823.cms.model.extension.Product;
import com.we823.cms.model.extension.ProductPhoto;
import com.we823.cms.repository.service.extension.BrandService;
import com.we823.cms.repository.service.extension.ProductCategoryService;
import com.we823.cms.repository.service.extension.ProductPhotoService;
import com.we823.cms.repository.service.extension.ProductService;

@Controller
public class ProductController {
	private String baseUrl = WebConstants.WEB_TEMPLATE_PREFIX;
	
	private @Resource ProductService productService;
	private @Resource ProductCategoryService productCategoryService;
	private @Resource BrandService brandService;
	private @Resource ProductPhotoService productPhotoService;
	
	@RequestMapping("products")
	public String products(HttpServletRequest request,Model model){
		Integer id = RequestUtil.getValueByName(request, "id", 0);
		model.addAttribute("category_id",id);
		
		String category_name = "产品目录";
		Integer parent_id = 0;
		
		String keyword = RequestUtil.getValueByName(request, "keyword");
		if(!StringUtils.isEmpty(keyword)) {
			model.addAttribute("keyword",keyword);
			model.addAttribute("autoSearch",1);
		}else {
			model.addAttribute("autoSearch",0);
		}
		if(id>0){
			ProductCategory productCategoryModel = productCategoryService.findOne(id);
			if(productCategoryModel!=null){
				category_name=productCategoryModel.getName();
				if(productCategoryModel.getParent()!=null){
					parent_id = productCategoryModel.getParent().getId();
				}
			}
		}
		
		
		model.addAttribute("category_name",category_name);
		model.addAttribute("parent_id",parent_id);
		return FrontUtils.buildTemplateUrl(baseUrl, "products");
	}
	
	@RequestMapping("product_{id}")
	public String product(@PathVariable("id")Integer id,Model model){
		Product productModel = productService.findOne(id);
		if(productModel!=null){
			
			String description = productModel.getDescription();
			if (!StringUtils.isEmpty(description) && description.contains("\r\n")) {
				description = description.replaceAll("\r\n", "<br>");
				productModel.setDescription(description);
			}
			List<ProductPhoto> photoModels = productPhotoService.findByProductid(productModel.getId());
			if (photoModels!=null && photoModels.size()>0) {
				for (ProductPhoto productPhotoModel : photoModels) {
					String urlString = productPhotoModel.getUrl();
					urlString = fixPhotoUrl(urlString);
					productPhotoModel.setUrl(urlString);
				}
			}
			productModel.setPhotos(photoModels);
			
			model.addAttribute("product",productModel);
			
			List<Product> otherProducts = productService.findByCategory(productModel.getCategory());
			model.addAttribute("otherProducts",otherProducts);
			
			model.addAttribute("category",productModel.getCategory());
		}
		
		return FrontUtils.buildTemplateUrl(baseUrl, "product");
	}
	
	@RequestMapping("products_get")
	@ResponseBody
	public Model gets(HttpServletRequest request,Model model){
		Integer categoryId= RequestUtil.getValueByName(request, "category_id", 0);
		Integer parentId = RequestUtil.getValueByName(request, "parent_id", -1);
		
		List<Product> products = Lists.newArrayList();
		
		if(parentId==-1){
			products = productService.findAll();
		}else{
			if(parentId==0){
				List<ProductCategory> productCategoryModels = productCategoryService.findByParentid(categoryId);
				products = productService.findByCategory(productCategoryModels);
			}else{
				ProductCategory category = productCategoryService.findOne(categoryId);
				products = productService.findByCategory(category);
			}
		}
		if (products!=null) {
			for (Product productModel : products) {
				String urlString = productModel.getPhoto();
				urlString = fixPhotoUrl(urlString);
				productModel.setPhoto(urlString);
			}
		}
		model.addAttribute("products",products);
				
		return model;
	}

	/**
	 * 根据图片地址获取完整的地址
	 * @param photoUrl
	 * @return
	 */
	private String fixPhotoUrl(String photoUrl) {
		if (StringUtils.isEmpty(photoUrl)) {
			photoUrl = WebConstants.PHOTO_PREFIX + WebConstants.NOPIC;
		}else {
			if (!photoUrl.startsWith("/u/r")) {
				photoUrl = WebConstants.PHOTO_PREFIX + photoUrl;
			}
		}
		return photoUrl;
	}
	
	@RequestMapping("search")
	@ResponseBody
	public Model search(HttpServletRequest request,Model model) {
		String keyword = RequestUtil.getValueByName(request, "keyword");
		List<Product> productModels = productService.findByKeyword(keyword);
		if (productModels!=null && productModels.size()>0) {
			for (Product productModel : productModels) {
				String url = productModel.getPhoto();
				url = fixPhotoUrl(url);
				productModel.setPhoto(url);
			}
		}
		model.addAttribute("products",productModels);
		if (productModels==null || productModels.size()==0) {
			model.addAttribute("hasError",true);
			model.addAttribute("message","没有找到["+keyword+"]相关的产品");
			model.addAttribute("total",0);
		}else {
			model.addAttribute("hasError",false);
			model.addAttribute("total",productModels.size());
		}
		return model;
	}
	
}
